/*
 * $Id: OtherPendingChangelistsContainer.java 23 2002-09-29 13:59:30Z bpruessmann $
 *
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.internal.ui.model;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sourceforge.p4eclipse.core.PerforceException;
import net.sourceforge.p4eclipse.core.api.IChangesResult;
import net.sourceforge.p4eclipse.core.api.IInfoResult;
import net.sourceforge.p4eclipse.core.api.IOpenedResult;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
  * This class works as a container for pending changelists of all other clients.
  *
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class OtherPendingChangelistsContainer extends AbstractPendingChangelistsContainer
{
	/** @see IWorkbenchAdapter#getLabel(Object) */
	public String getLabel(Object o) 
	{ 
		if (!(o instanceof OtherPendingChangelistsContainer))
			return null;
			
		return "Pending Changelists (other clients)";
	}
	
	/** @see IWorkbenchAdapter#getImageDescriptor(Object) */
	public ImageDescriptor getImageDescriptor(Object o)
	{
		if (!(o instanceof OtherPendingChangelistsContainer))
			return null;
		
		return PerforceUIPlugin.getPlugin().getImageDescriptor(PerforceUIPlugin.IMG_OTHER_CHANGELISTS);
	}	
	
	/** @see IWorkbenchAdapter#getChildren(Object) */
	public Object[] getChildren(Object o)
	{
		if (!(o instanceof OtherPendingChangelistsContainer))
			return null;
		
		Map changesResultMap = new HashMap(); 
		try
		{			
			IInfoResult infoResult = PerforceUIPlugin.getClient().info();
			
			IChangesResult[] origChangesResult = PerforceUIPlugin.getClient().changes("pending", 100);
			for (int i = 0, length = origChangesResult.length; i < length; i++)
			{
				if (infoResult.getClientName().equals(origChangesResult[i].getClientName()))
					continue;
					
				String name = origChangesResult[i].getChangelist().toString() +
				              " " + origChangesResult[i].getUserName() + "@" + origChangesResult[i].getClientName(); //$NON-NLS-1$ //$NON-NLS-2$
				
				changesResultMap.put(name, new OtherChangesResult(origChangesResult[i]));				
			} 
			
			
			IOpenedResult[] openedResult = PerforceUIPlugin.getClient().opened(true);
			for (int i = 0, length = openedResult.length; i < length; i++)
			{
				if (infoResult.getClientName().equals(openedResult[i].getClientName()))
					continue;
					
				String name = openedResult[i].getChangelist().intValue() == -1 ? 
				              "default" : openedResult[i].getChangelist().toString(); //$NON-NLS-1$
				name += " " + openedResult[i].getUserName() + "@" + openedResult[i].getClientName(); //$NON-NLS-1$ //$NON-NLS-2$

				OtherChangesResult changesResult = (OtherChangesResult)changesResultMap.get(name);
				if (changesResult == null)
				{
					changesResult = new OtherChangesResult(openedResult[i]);
					changesResultMap.put(name, changesResult);
				}
				
				changesResult.openedResultList.add(openedResult[i]);
			}
		}
		catch (PerforceException e)
		{
			PerforceUIPlugin.getPlugin().log(e.getStatus());
		}
		
		return changesResultMap.values().toArray();
	}

	/**
	  * This class repersents the result of a 'p4 changes' command as it might
	  * have been returned for other clients.
	  */	
	private static class OtherChangesResult implements IChangesResult, IWorkbenchAdapter
	{
		private Integer changelist;
		private String clientName;
		private String userName;
		private String description;
		
		private List openedResultList = new ArrayList();
		
		public OtherChangesResult(IOpenedResult openedResult)
		{
			this.changelist = openedResult.getChangelist();
			this.clientName = openedResult.getClientName();
			this.userName = openedResult.getUserName();			
			this.description = ""; //$NON-NLS-1$
		}
		
		public OtherChangesResult(IChangesResult changesResult)
		{
			this.changelist = changesResult.getChangelist();
			this.clientName = changesResult.getClientName();
			this.userName = changesResult.getUserName();
			this.description = 	changesResult.getDescription();
		}		
		
		/** @see IChangesResult#getChangelist() */
		public Integer getChangelist() { return changelist; }

		/** @see IChangesResult#getClientName() */
		public String getClientName() { return clientName; }

		/** @see IChangesResult#getDate() */
		public Date getDate() { return new Date(); }

		/** @see IChangesResult#getDescription() */
		public String getDescription() { return description; }

		/** @see IChangesResult#getStatus() */
		public String getStatus() { return "pending"; }

		/** @see IChangesResult#getUserName() */
		public String getUserName() { return userName; }

		/** @see IWorkbenchAdapter#getChildren(Object) */
		public Object[] getChildren(Object o) 
		{ 
			if (!(o instanceof OtherChangesResult))
				return null; 

			return openedResultList.toArray();
		}

		/** @see IWorkbenchAdapter#getImageDescriptor(Object) */
		public ImageDescriptor getImageDescriptor(Object o) 
		{ 
			if (!(o instanceof OtherChangesResult))
				return null; 
			
			return PerforceUIPlugin.getPlugin().getImageDescriptor(PerforceUIPlugin.IMG_OTHER_CHANGELIST);
		}

		/** @see IWorkbenchAdapter#getLabel(Object) */
		public String getLabel(Object o) 
		{ 
			if (!(o instanceof OtherChangesResult))
				return null; 
			
			String pattern = changelist.intValue() == -1 ?
			                 "Default - {1}@{2}" : "Change {0,number,#####} - {1}@{2} [{3}]";
			                 
			String shortendDescription = description;
			if (shortendDescription.length() >= 40)
				shortendDescription = shortendDescription.substring(0, 39) + "..."; //$NON-NLS-1$			                 
						              
			Object[] arguments = new Object[4];
			arguments[0] = changelist;
			arguments[1] = userName;
			arguments[2] = clientName;
			arguments[3] = shortendDescription;
			              
			return MessageFormat.format(pattern, arguments); 
		}

		/** @see IWorkbenchAdapter#getParent(Object) */
		public Object getParent(Object o) { return null; }

		/** @see IAdaptable#getAdapter(Class)  */
		public Object getAdapter(Class adapter)
		{
			return (adapter == IWorkbenchAdapter.class) ? this : null; 
		}
	}	
}
